<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use app\common\model\Menu;
use app\common\model\WholesaleProduct;
use app\common\model\WholesaleOrder;
use app\common\model\Role;
use app\common\tool\Result;
use think\facade\Cache;
use think\facade\Template;
use think\facade\View;
use think\Response;
use addons\Target\facade\Target;

class Index extends Base
{

    protected array $sort = [];

    /**
     * 显示资源列表
     *
     * @return string
     */
    public function index(): string
    {
        // $v= WholesaleProduct::where('id',1)->find();
        // $data = WholesaleOrder::where(['type' => 1, 'product_id' => $v->id, 'status' => 4, 'is_medal' => 0])->whereDay('create_time')->column('id');
        // $win_data = WholesaleOrder::where(['type' => 1, 'product_id' => $v->id, 'status' => 4, 'is_medal' => 1])->whereDay('create_time')->column('id');
        // $list = get_prize($data, $win_data, $v->win_bl, $v->fail_bl);
        // print('<pre>');print_r($list);die;
        // Target::Account()->basics(2, 'bonus_4', '2', '购买燃料积分商品', '+')
        // ->setIsGrant(1)
        // ->createBonus(0,1,false);
        $rules = '';
        if ($this->admin['role_id'] != 1) {
            $rules = (new Role())->where('id', $this->admin['role_id'])->value('rules');
        }
        View::assign([
            'menus'      => Menu::getNewMenu(1, $rules),
            'rules'      => $this->admin['role_id'] != 1 ? explode(',',$rules):[],
            'admin_id'      => $this->admin->id,
            'attachMenu' => $this->admin['role_id'] == 1 ? Builder::TableCache()->getAttachMenu() : []
        ]);
        return View::fetch();
    }

    public function welcome(): string
    {
        $lnk = [
            [
                'title' => '正式会员',
                'icon'  => '/static/admin/images/huiyuan.png',
                'url'   => url('user/formal')->build()
            ],
            [
                'title' => '充值管理',
                'icon'  => '/static/admin/images/chongzhi.png',
                'url'   => url('remit/index')->build()
            ],
            [
                'title' => '提现管理',
                'icon'  => '/static/admin/images/tixian.png',
                'url'   => url('draw/index')->build()
            ],
            [
                'title' => '转账管理',
                'icon'  => '/static/admin/images/zhuanzhang.png',
                'url'   => url('transfer/index')->build()
            ],
            [
                'title' => '转换管理',
                'icon'  => '/static/admin/images/zhuanhuan.png',
                'url'   => url('exchange/index')->build()
            ],
            [
                'title' => '商品管理',
                'icon'  => '/static/admin/images/shangpin.png',
                'url'   => url('product/index')->build()
            ],
            [
                'title' => '流水明细',
                'icon'  => '/static/admin/images/liushui.png',
                'url'   => url('account/index')->build()
            ],
            [
                'title' => '订单列表',
                'icon'  => '/static/admin/images/renwu.png',
                'url'   => url('order/index')->build()
            ]
        ];

        View::assign([
            'server_name' => $_SERVER['SERVER_NAME'],
            'lnk'         => $lnk
        ]);
        return View::fetch();
    }

    public function clearCache(): Response
    {
        return Cache::clear() ? Result::success('已清除') : Result::fail('加载失败');
    }

    public function getSetting(): Response
    {
        $result = [
            'btn_style' => cache('btn_style') ?: 1,
            'layout'    => cache('layout') ?: 1,
        ];

        return Result::success('success', $result);
    }

    public function setSetting(): Response
    {
        try {
            $param = $this->request->post();
            cache($param['key'], $param['value']);
            return Result::success('设置成功，刷新页面后生效');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

}
